﻿namespace OutLook.Module.Load
{
    using System;
    using System.Data;
    using System.Resources;
    using OutLook.Model.Domains;
    using OutLook.Model.Infrastructure;
    using OutLook.Module.Infrastructure;
    using OutLook.Module.Infrastructure.Interfaces;
    using OutLook.Module.Load.Contracts;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class TechnologyController : DomainControllerBase, ITechnologyContract
    {
        #region Constructors

        public TechnologyController(IRepository context)
            : base(context) { }


        #endregion

        #region Properties

        public Technology CurrentTechnology
        {
            get { return m_CurrentTechnology; }
            set { m_CurrentTechnology = value; }
        }

        #endregion

        #region DomainControllerBase members

        protected override void Dispose(bool dispoceManged)
        {
            if (m_Disposed)
                return;

            m_Disposed = true;
        }

        #endregion

        #region ITechnologyContract Implementation

        public System.Data.DataTable DownloadTheTechnology(string fileName)
        {
            m_CurrentTechnology= DataSerializerHelper.XmlRead<Technology>(fileName);
            CalcTehnologyParametrs();
            DataTable result = new DataTable();
            ResourceManager rm=new ResourceManager(typeof(OutLook.Model.Domains.ResourceInformation.Technology));

            result.Columns.Add(Constants.ROW_NUMBER, typeof(string));
            result.Columns.Add(OutLook.Model.Domains.ResourceInformation.Technology.TechnologyParamName, typeof(string));
            result.Columns.Add(OutLook.Model.Domains.ResourceInformation.Technology.TechnologyParamValue, typeof(double));

            ///Rows
            for (int i = 0; i < m_CurrentTechnology.GetType().GetProperties().Length; i++)
            {
                object[] chargeInRow = new object[result.Columns.Count];
                chargeInRow[0] = i + 1;
                chargeInRow[1] = rm.GetString(m_CurrentTechnology.GetType().GetProperties()[i].Name);
                chargeInRow[2] = m_CurrentTechnology.GetType().GetProperties()[i].GetValue(m_CurrentTechnology, null);               
                result.Rows.Add(chargeInRow);
            }

            return result;
        }

        public void CalcTehnologyParametrs()
        {
            double fi = m_CurrentTechnology.NaturalWet / 100 / 8.04;
            double fi_dut = fi + (m_CurrentTechnology.SteamOutlay * 1000000) / 60 / (22.4 * m_CurrentTechnology.BlastOutlay) / 18000;
            double vldut = fi_dut * 100 * 8.04;


            double vvdd = 0.933 / (m_CurrentTechnology.BlastO2Content / 100 + 0.5 * fi_dut);
            double vg = 1.8667 + vvdd * (1 - m_CurrentTechnology.BlastO2Content / 100 + fi_dut);
            double v_d = (0.5 * m_CurrentTechnology.NaturalGasCG - m_CurrentTechnology.NaturalGasOG) / (m_CurrentTechnology.BlastO2Content / 100 + 0.5 * fi_dut);

            double v_g = m_CurrentTechnology.NaturalGasCG + m_CurrentTechnology.NaturalGasHG + m_CurrentTechnology.NaturalGasNG + v_d * (1 - m_CurrentTechnology.BlastO2Content / 100 + fi_dut);
            double dg = m_CurrentTechnology.NaturalGasOutlay / m_CurrentTechnology.BlastOutlay;
            double sig = vvdd * dg / (1 - v_d * dg);
            double cfo = (m_CurrentTechnology.BlastOutlay - m_CurrentTechnology.NaturalGasOutlay * v_d) / vvdd;
            double vdsu = vvdd + sig * v_d;
            double vfgsu = vg + sig * v_g;
            double vfgo = vfgsu * cfo;
            double vsek = vfgo / 60 / m_CurrentTechnology.TuyeresQuantity;
            double vco = 1.8667 + sig * m_CurrentTechnology.NaturalGasCG;
            double vh2 = vdsu * fi_dut + sig * m_CurrentTechnology.NaturalGasHG;
            double vn2 = vdsu * (1 - m_CurrentTechnology.BlastO2Content / 100) + sig * m_CurrentTechnology.NaturalGasNG;
            double plot = 1.25 - 1.16 * vh2 / vfgsu;
            double c0 = -25.6 + 0.37077 * m_CurrentTechnology.BlastTemperature;
            double h20 = -46.005 + 0.47691 * m_CurrentTechnology.BlastTemperature;
            double id = c0 - fi_dut * (2580 - h20);

            double ws = 3.8 * Constants.CH4PG + 14.5 * Constants.C2H6PG + 24.2 * Constants.C3H8PG;
            ws = ws + 33 * Constants.C4H10PG + 43 * Constants.C5H12PG + 45 * Constants.C6H14PG;
            ws = ws + 45 * Constants.C7H16PG - 30.2 * Constants.CO2PG - 25.8 * Constants.H2OPG;
            double c0t = (2340 + 591 + vvdd * id + sig * (ws + 20 + v_d * id)) / vfgsu;

            double m3 = (100 * ((m_CurrentTechnology.NaturalGasOutlay / 60) / m_CurrentTechnology.BlastOutlay));
            double m4 = 8 + (m_CurrentTechnology.SteamOutlay * 1000000) / (60 * m_CurrentTechnology.BlastOutlay);

            double t = 0.9341 * m_CurrentTechnology.BlastTemperature + 8208 * (m_CurrentTechnology.BlastO2Content / 100) - (2402 - 1.2127 * m_CurrentTechnology.BlastTemperature) * fi_dut - 2792 * (m_CurrentTechnology.NaturalGasOutlay / m_CurrentTechnology.BlastOutlay) + 1.0289 * 30 * (m_CurrentTechnology.NaturalGasOutlay / m_CurrentTechnology.BlastOutlay) + 94.74;
            m_CurrentTechnology.Ttb = t / ((1 + m_CurrentTechnology.BlastO2Content / 100) + 2 * fi_dut + 2.077 * (m_CurrentTechnology.NaturalGasOutlay / m_CurrentTechnology.BlastOutlay));
            m_CurrentTechnology.Ttb = Math.Round(m_CurrentTechnology.Ttb, 0);

            double sfu = Math.Pow((m_CurrentTechnology.TuyeresDiameter / 1000), 2) * 3.14 / 4;
            double d0k = m_CurrentTechnology.CokeDiameter / 1000;

            m_CurrentTechnology.Ltz = 0.315 * m_CurrentTechnology.TuyeresDiameter / 1000 * Math.Pow((plot * Math.Pow((vsek / sfu), 2) * (m_CurrentTechnology.Ttb + 273.15) / 273.15 / m_CurrentTechnology.BlastPressure / 9.81 / d0k / m_CurrentTechnology.CokeDensity), 0.587);
            m_CurrentTechnology.Ltz = Math.Round(m_CurrentTechnology.Ltz, 3);

            m_CurrentTechnology.Eta = Math.Round((m_CurrentTechnology.TopGasCO2 / (m_CurrentTechnology.TopGasCO2 + m_CurrentTechnology.TopGasCO)), 3);
        }
        
        #endregion

        #region Fields

        private bool m_Disposed = false;
        private Technology m_CurrentTechnology = null;

        #endregion        
    }
}
